import re
from collections import OrderedDict


class NUMBER:
    @staticmethod
    def hex2dec(_str):
        return str(int(_str, 16))

    single_num_dict = {
        u'零': 0,
        u'一': 1,
        u'二': 2,
        u'三': 3,
        u'四': 4,
        u'五': 5,
        u'六': 6,
        u'七': 7,
        u'八': 8,
        u'九': 9,
        u'十': 10
    }

    @staticmethod
    def chinese2num(chinese):
        reg = u'[十百千]'
        e = re.compile(reg)
        result_list = e.findall(chinese)
        if len(result_list) > 0:
            tmp = None
            ge = 0
            num = OrderedDict()
            num[u"十"] = None
            num[u"百"] = None
            num[u"千"] = None

            if chinese[:1] == '十':
                chinese = '一' + chinese

            for letter in chinese:
                if letter != '十' and letter != '百' and letter != '千':
                    tmp = NUMBER.single_num_dict[letter]
                else:
                    num[letter] = tmp
                    tmp = None

            if tmp is not None:
                index = 0
                for key in num:
                    if num[key] is not None:
                        break
                    index += 1

                if index <= 0:
                    ge = tmp
                else:
                    bingo_index = index - 1
                    index = 0
                    for key in num:
                        if index == bingo_index:
                            num[key] = tmp
                            break
                        index += 1

            for key in num:
                if num[key] is None:
                    num[key] = 0

            return str(num[u"十"] * 10 + num[u"百"] * 100 + num[u"千"] * 1000 + ge)
        else:
            result = ''
            for letter in chinese:
                result += str(NUMBER.single_num_dict[letter])
            return result
